#!/bin/env python3
from flask import Flask, render_template, send_from_directory
import argparse
import socketserver
import logging as log
import json



def run_server(port: int=8000):
    log.info("Starting server")
    app = Flask(__name__, template_folder=".")

    @app.route("/")
    def index():
        return render_template("index.html")

    @app.route("/<path:name>")
    def files(name):
        print(name)
        return send_from_directory(
            "", name, as_attachment=True
        )
    
    @app.route("/api/claim_task", methods=["POST"])
    def claim_task():
        return '{"1": "ABOBA"}'

    app.run(port=port)

if __name__ == "__main__":
    log.basicConfig(filename='server.log', filemode='w', level=log.INFO, format='[%(levelname)s] %(message)s')
    
    parser = argparse.ArgumentParser(prog="Math Leaderboard", description="Runs web server that shows math rank")
    parser.add_argument("-p", "--port", type=int, default=8000, dest="port")
    args = parser.parse_args()

    run_server(args.port)
